<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/3/31
 * Time: 17:09
 */

namespace common\sources\read\businesspepperrecord;


use common\models\craftsman\BusinessPepperRecord;

class BusinessPepperRecordAccess implements IBusinessPepperRecordAccess
{
    public function getCount($data)
    {
        $where = $this->getWhere($data);
        return BusinessPepperRecord::find()
                ->where($where)
                ->count();
    }

    public function getList($data)
    {
        $where = $this->getWhere($data);
        return BusinessPepperRecord::find()
                ->select("id,task_id,task_order_num,expend,income,balance,remark,create_time")
                ->where($where)
                ->orderBy("id desc")
                ->offset(($data['page_num']-1)*$data['limit'])
                ->limit($data['limit'])
                ->asArray()
                ->all();
    }

    public function getWhere($data)
    {
        $sql = " bus_id = ".$data['bus_id'];
        if(!empty($data['task_id']) && isset($data['task_id']))
        {
            $sql .= " AND task_id = '".$data['task_id']."'";
        }
        if(!empty($data['start_time']) && isset($data['start_time']))
        {
            $sql .= " AND create_time >= ".$data['start_time'];
        }
        if(!empty($data['end_time']) && isset($data['end_time']))
        {
            $sql .= " AND create_time <= ".$data['end_time'];
        }
        return $sql;
    }

    public function getRechargeCount($data)
    {
        return BusinessPepperRecord::find()
            ->where("bus_id =". $data['bus_id']." AND type =1")
            ->count();
    }

    public function getRechargeList($data)
    {
        return BusinessPepperRecord::find()
            ->select("id,income,remark,create_time")
            ->where("bus_id =". $data['bus_id']." AND type =1")
            ->orderBy("id desc")
            ->offset(($data['page_num']-1)*$data['limit'])
            ->limit($data['limit'])
            ->asArray()
            ->all();
    }

}